import axios from 'axios'
import queryString from 'querystring'
import Vue from 'vue'
import store from '../store/index'
import router from '../router/index'

const instance = axios.create({
  baseURL: 'http://47.105.186.204:70',
  timeout: 50000,
  headers: {}
})

//添加请求拦截器
instance.interceptors.request.use(function(config) {
  // 请求头添加token
  if (store.state.UserToken) {
    config.headers.token = store.state.UserToken
  }
  return config
})

// 拦截报错提示
instance.interceptors.response.use(
  response => {
    if (response && response.data.code == 500) {
      Vue.prototype.$Message.error({
        content: response.data.msg,
        duration: 3
      })
      if (response.data.msg == '用户未登录!') {
        // Vue.prototype.$Message.warning({
        //     message: '用户未登录'
        // })
        router.push({ path: '/login' })
        return
      }
    } else if (response.data.code == -100) {
      Vue.prototype.$Message.error({
        content: response.data.msg,
        duration: 3
      })
    }
    // if(response.data.result==1){
    //     Vue.prototype.$Message.success({
    //         content:response.data.msg,
    //         duration:3
    //     })
    // }
    return response.data
  },
  err => {
    if (err && err.response) {
      switch (err.response.status) {
        case err.response.msg == '用户未登录!':
          Vue.prototype.$Message.warning({
            message: '用户未登录'
          })
          Vue.prototype.$router.go('login')
          return
      }
    } else {
      err.message = '服务器错误'
    }
    err.message = '未知错误'
    Vue.prototype.$Message.error(err.message)
  }
)

//get 请求列表
const getConfig = {
  gettest() {
    instance.get('/gjz?page=').then(data => callback(data))
  },
  loginout() {
    return instance.get('/logout')
  }
}
const postConfig = {
  login(params) {
    return instance.post('/login', params)
  },
  gettest(params) {},
  queryPageQy(params) {
    return instance.post('/company/queryPage', params)
  },
  updataQy(params) {
    return instance.post('/company/update', params)
  },
  deleteQy(params) {
    return instance.post('/company/delete', params)
  },
  itemQueryPage(params) {
    return instance.post('/item/queryPage', params)
  },
  candidate_queryPage(params) {
    return instance.post('/item/candidate/queryPage', params)
  },
  evaluation_queryPage(params) {
    return instance.post('/evaluation/queryPage', params)
  },
  evaluation_update(params) {
    return instance.post('/evaluation/update', params)
  },
  //测评管理 删除
  evaluation_delete(params) {
    return instance.post('/evaluation/delete', params)
  },
  //保存测评用户信息
  voteSaveUser(params) {
    return instance.post('/vote/vote_saveUser.action', params)
  },
  retrieve(params) {
    return instance.post('/user/retrieve', params)
  },
  upload() {
    return instance.post('/upload', params)
  }
}

// 普通暴露，接收的时候用大括号
export const get = function(fnName, params) {
  return getConfig[fnName](params)
}

export const post = function(fnName, params) {
  return postConfig[fnName](params)
}

// 默认暴露，接收的时候不用大括号
export default instance
